#pragma once
#include "config.hpp"
#include <unordered_map>
#include <pthread.h>

struct BackupInfo
{
    bool pack_flag;
    size_t fsize;
    time_t mtime;
    time_t atiem;
    string real_path;
    string pack_path;
    string url;

    bool NewBackupInfo(const string &realpath)
    {
        Config *ptr=Config::GetConfigObj();
        cloudBackup::FileUtil fu(realpath);
        if(fu.Exists()==false)
            return false;
        pack_flag=false;
        fsize=fu.FileSize();
        mtime=fu.LastModFile();
        atiem=fu.LastAccFile();
        real_path=realpath;
        pack_path=ptr->GetPackDir()+fu.FileName()+ptr->GetPackSuffix();
        url=ptr->GetDownloadPrefix()+fu.FileName();
        return true;
    }
};

class DataManager
{
    private:
        string _backup_file;
        pthread_rwlock_t _rwlock;
        unordered_map<string,BackupInfo> _table;
    public:
        DataManager();
        ~DataManager();
        bool Insert(const BackupInfo &info);
        bool Updata(const BackupInfo &info);
        bool GetOneByURL(const string &url,BackupInfo *info);
        bool GetOneByRealPath(const string &realpath,BackupInfo *info);
        bool GetAll(vector<BackupInfo> *arry);
        bool Storage(); 
        bool InitLoad();
};


